﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    // Initialize and Start the game
    document.addEventListener("DOMContentLoaded", initialize, false);

    // THE GAME
    var scaleW = window.innerWidth / 1366;
    var scaleH = window.innerHeight / 768;

    var preload;
    var canvas, context, stage;

    var newGame = true;

    var logoScreenImage, logoScreenBitmap;
    var floorImage, floorBitmap;
    var playerIdleImage, playerIdleBitmap;
    var playerWalk0Image, playerWalk0Bitmap;
    var playerWalk1Image, playerWalk1Bitmap;
    var ghostImage, ghostBitmap;

    var player;
    var ghosts = [];
    var ghostSpeed = 1.0;

    var timeToAddNewGhost = 0;
    var isGameOver = false;

    var scoreText;
    var highScoreText;
    var playerScore = 0;
    var playerHighScore = 0;

    function Player() {
        this.positionX = window.innerWidth / 2;
        this.positionY = window.innerHeight / 2;

        this.targetX = this.positionX;
        this.targetY = this.positionY;

        this.width = playerIdleBitmap.image.width * scaleW;
        this.height = playerIdleBitmap.image.height * scaleH;
    }

    function Ghost(gfx) {
        this.positionX = Math.random() * 5000 - 2500;
        this.positionY = Math.random() * 3000 - 1500;

        this.setStartPosition = function () {
            if (this.positionX >= 0 && this.positionX <= window.innerWidth) {
                this.positionX = -500;
            }

            if (this.positionY >= 0 && this.positionY <= window.innerHeight) {
                this.positionY = -500;
            }
        }

        this.targetX = 0;
        this.targetY = 0;

        this.move = function (tX, tY) {
            this.targetX = tX;
            this.targetY = tY;

            if (this.targetX > this.positionX) {
                this.positionX += ghostSpeed;
            }
            if (this.targetX < this.positionX) {
                this.positionX -= ghostSpeed;
            }
            if (this.targetY > this.positionY) {
                this.positionY += ghostSpeed;
            }
            if (this.targetY < this.positionY) {
                this.positionY -= ghostSpeed;
            }
        };

        this.isCollision = function (playerX, playerY, playerW, playerH) {
            var centerX = this.positionX + (this.ghostBitmap.image.width * scaleW / 2);
            var centerY = this.positionY + (this.ghostBitmap.image.height * scaleH / 2);

            if ((centerX >= playerX - playerW / 2) && (centerX < playerX + playerW / 2)) {
                if ((centerY >= playerY - playerH / 2) && (centerY < playerY + playerH / 2)) {
                    return true;
                }
            }

            return false;
        }

        this.ghostBitmap = gfx;
    }



    function pointerUp(event) {
        if (newGame && !isGameOver) {
            newGame = false;
        }
        else {
            player.targetX = event.x;
            player.targetY = event.y;
        }

        if (isGameOver) {
            isGameOver = false;
            newGame = true;
        }
    }

    function pointerDown(event) {
        if (newGame && !isGameOver) {
        }
        else {
            player.targetX = event.x;
            player.targetY = event.y;
        }
    }

    function pointerMove(event) {
        if (newGame && !isGameOver) {
        }
        else {
            player.targetX = event.x;
            player.targetY = event.y;
        }
    }

    function initialize() {
        var appData = Windows.Storage.ApplicationData.current;
        var roamingSettings = appData.roamingSettings;
        var tempHighScore = roamingSettings.values["highscore"];
        if (!tempHighScore) {
        }
        else {
            playerHighScore = tempHighScore;
        }


        canvas = document.getElementById("canvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        context = canvas.getContext("2d");

        canvas.addEventListener("MSPointerUp", pointerUp, false);
        canvas.addEventListener("MSPointerMove", pointerMove, false);
        canvas.addEventListener("MSPointerDown", pointerDown, false);

        stage = new createjs.Stage(canvas);

        preload = new createjs.PreloadJS();
        preload.onComplete = prepareGame;
        var manifest = [
            { id: "logoScreen", src: "images/GFX/LogoScreen.png" },
            { id: "floor", src: "images/GFX/floor.png" },
            { id: "ghost", src: "images/GFX/Ghost.png" },
            { id: "playerIdle", src: "images/GFX/PlayerIdle.png" },
            { id: "playerWalk0", src: "images/GFX/PlayerWalk0.png" },
            { id: "playerWalk1", src: "images/GFX/PlayerWalk1.png" }
        ];

        preload.loadManifest(manifest);
    }

    function prepareGame() {
        logoScreenImage = preload.getResult("logoScreen").result;
        logoScreenBitmap = new createjs.Bitmap(logoScreenImage);
        logoScreenBitmap.scaleX = scaleW;
        logoScreenBitmap.scaleY = scaleH;
        stage.addChild(logoScreenBitmap);

        floorImage = preload.getResult("floor").result;
        floorBitmap = new createjs.Bitmap(floorImage);
        floorBitmap.visible = false;
        floorBitmap.scaleX = scaleW;
        floorBitmap.scaleY = scaleH;
        stage.addChild(floorBitmap);

        playerIdleImage = preload.getResult("playerIdle").result;
        playerIdleBitmap = new createjs.Bitmap(playerIdleImage);
        playerIdleBitmap.visible = false;
        playerIdleBitmap.scaleX = scaleW;
        playerIdleBitmap.scaleY = scaleH;
        stage.addChild(playerIdleBitmap);


        scoreText = new createjs.Text("Score: " + "0", "72px segoe ui", "white");
        scoreText.x = canvas.width / 2 - (scoreText.getMeasuredWidth() * scaleW / 2);
        scoreText.scaleX = scaleW;
        scoreText.scaleY = scaleH;
        scoreText.y = 30 * scaleH;
        scoreText.visible = false;
        stage.addChild(scoreText);


        highScoreText = new createjs.Text("Highscore: " + "0", "42px segoe ui", "white");
        highScoreText.x = canvas.width / 2 - (scoreText.getMeasuredWidth() * scaleW / 2);
        highScoreText.scaleX = scaleW;
        highScoreText.scaleY = scaleH;
        highScoreText.y = 100 * scaleH;
        highScoreText.visible = false;
        stage.addChild(highScoreText);


        ghostImage = preload.getResult("ghost").result;

        player = new Player();

        stage.update();

        startGame();
    }

    function startGame() {
        createjs.Ticker.setInterval(window.requestAnimationFrame);
        createjs.Ticker.addListener(gameLoop);
    }

    function gameLoop() {
        update();
        draw();
    }

    function update() {
        if (newGame) {
            logoScreenBitmap.visible = true;
            floorBitmap.visible = false;
            playerIdleBitmap.visible = false;
            scoreText.visible = false;
            highScoreText.visible = false;
        }
        else {
            if (isGameOver) {
                if (playerHighScore < playerScore) {
                    playerHighScore = playerScore;

                    var appData = Windows.Storage.ApplicationData.current;
                    var roamingSettings = appData.roamingSettings;
                    roamingSettings.values["highscore"] = playerHighScore;
                }

                isGameOver = false;
                playerScore = 0;
                ghosts.length = 0;
                stage.clear();

                stage.addChild(logoScreenBitmap);
                stage.addChild(floorBitmap);
                stage.addChild(playerIdleBitmap);
                stage.addChild(scoreText);
                stage.addChild(highScoreText);

                stage.update();
            }

            logoScreenBitmap.visible = false;
            floorBitmap.visible = true;
            playerIdleBitmap.visible = true;
            scoreText.visible = true;
            highScoreText.visible = true;

            if (player.targetX > player.positionX) {
                player.positionX += 3;
            }
            if (player.targetX < player.positionX) {
                player.positionX -= 3;
            }
            if (player.targetY > player.positionY) {
                player.positionY += 3;
            }
            if (player.targetY < player.positionY) {
                player.positionY -= 3;
            }

            playerScore += 1;
            scoreText.x = canvas.width / 2 - (scoreText.getMeasuredWidth() * scaleW / 2);
            highScoreText.x = canvas.width / 2 - (highScoreText.getMeasuredWidth() * scaleW / 2);
            scoreText.text = ("Score: " + playerScore);
            highScoreText.text = ("Highscore: " + playerHighScore);

            timeToAddNewGhost -= 1;
            if (timeToAddNewGhost < 0) {
                timeToAddNewGhost = 1000
                ghosts.push(new Ghost(new createjs.Bitmap(ghostImage)));
                ghosts[ghosts.length - 1].setStartPosition();
                stage.addChild(ghosts[ghosts.length - 1].ghostBitmap);
            }

            for (var i = 0; i < ghosts.length; i++) {
                ghosts[i].ghostBitmap.x = ghosts[i].positionX;
                ghosts[i].ghostBitmap.y = ghosts[i].positionY;
                ghosts[i].ghostBitmap.visible = true;
                ghosts[i].move(player.positionX, player.positionY);
                isGameOver = ghosts[i].isCollision(player.positionX, player.positionY, player.width, player.height);
                if (isGameOver) {
                    break;
                }
            }


            playerIdleBitmap.x = player.positionX - (player.width / 2);
            playerIdleBitmap.y = player.positionY - (player.height / 2);
        }
    }

    function draw() {
        stage.update();
    }
})();
